package com.youdianstar.app.ui.reward

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseFragment
import com.youdianstar.app.common.network.BaseApiPagingObserver
import com.youdianstar.app.common.widget.adapter.FooterAdapter

import com.youdianstar.app.adapter.PointsPagingAdapter
import com.youdianstar.app.databinding.FragmentPointsListBinding

import kotlinx.coroutines.flow.collectLatest
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: PointRsFragment
 * Author: 82149 Create: 2022/3/2 16:52
 * Changes (from 2022/3/2)
 * --------------------------------------------------
 * description: 积分记录
 * ---------------------------------------------------
 *
 */
class PointRsFragment :BaseFragment<FragmentPointsListBinding, RewardViewModel>(){

    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_points_list
    }

    override fun initVariableId(): Int {
        return BR.fragModel
    }

    override fun initViewModel(): RewardViewModel {
        val model: RewardViewModel by viewModel()
        return model
    }

    companion object {
        fun newInstance(type: Int, titleTag: String): PointRsFragment {
            val bundle = Bundle()
            bundle.putInt(Extra.IT_TYPE, type)
            bundle.putString("Tag", titleTag)
            val fragment = PointRsFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    // 1-增加 2-使用
    private var dataType = 0

    private var mPagingAdapter: PointsPagingAdapter? = null

    override fun initData() {
        val type = arguments?.getInt(Extra.IT_TYPE)
        val tag = arguments?.getString("Tag")
        when (type) {
            0 -> dataType = 1
            1 -> dataType = 2
        }
        //Note : 类型多语言 从接口拿
        mPagingAdapter = PointsPagingAdapter(activity?.baseContext!!,dataType)

        binding.run {
            rcList.adapter = mPagingAdapter?.withLoadStateFooter(FooterAdapter {
                //重新请求
                mPagingAdapter?.retry()
            })
            //刷新监听
            swPoints.setOnRefreshListener {
                mPagingAdapter?.refresh()
            }
            lifecycleScope.launchWhenCreated {
                mPagingAdapter?.loadStateFlow?.collectLatest {
                    swPoints.isRefreshing = it.refresh is LoadState.Loading
                }
            }

            loadListData(dataType)
        }
    }

    override fun initClickEvent() {

    }

    override fun initLiveObserver() {
        viewModel.pointsPagingLiveData.observe(this, object : BaseApiPagingObserver(binding.rcList){
            override fun onReload(v: View?) {
                loadListData(dataType)
            }
        })
    }

    /**
     * 获取数据
     * @param type  1-增加 2-使用
     */
    private fun loadListData(type :Int){
        lifecycleScope.launchWhenCreated {
            viewModel.getPointRecordPaging(type).collectLatest {
                //要测试这里是否能 获取到数据结果
                mPagingAdapter?.submitData(it)
            }
        }
    }
}